<template>
  <view class="search-container">
    <view class="search-box">
      <!-- 基本用法 -->
      <uni-search-bar
        :radius="100"
        @input="input"
        cancelButton="none"
      ></uni-search-bar>
      <!-- 商品结果 -->
      <view class="sugg-list" v-if="goodsList.length > 0">
        <view
          class="sugg-item"
          v-for="item in goodsList"
          :key="item.goods_id"
          @click="clickGood(item.goods_id)"
        >
          <text class="goods-name">{{ item.goods_name }}</text>
        </view>
      </view>
      <!-- 搜索历史 -->
      <view class="history-box" v-else>
        <view class="history-title">
          <text>搜索历史</text>
          <uni-icons type="trash" size="17" @click="clearHistory"></uni-icons>
        </view>
      </view>
      <!-- 历史商品 -->
      <view class="history-list">
        <uni-tag
          type="default"
          v-for="item in historyList"
          :key="item"
          circle
          :text="item"
        ></uni-tag>
      </view>
    </view>
  </view>
</template>


<script>
export default {
  data() {
    return {
      goodsList: [], // 商品列表
      historyList: [], // 搜索历史
    };
  },
  onLoad() {
    this.historyList = uni.getStorageSync("history");
  },
  methods: {
    // 点击input
    input(e) {
      clearTimeout(this.timer);

      this.timer = setTimeout(() => {
        this.getGoodsListData(e);
      }, 500);
    },
    // keyword 输入的值
    async getGoodsListData(keyword) {
      if (keyword.trim().length === 0) return;

      const res = await uni.$request({
        url: "goods/qsearch",
        data: {
          query: keyword,
        },
      });
      console.log("res:", res);

      if (res.meta.status === 200) {
        this.goodsList = res.message;
      }

      this.saveHistory(keyword);
    },
    // 点击商品跳转页面
    clickGood(goods_id) {
      uni.navigateTo({
        url: "/subpkg/goods-detail/index?goods_id=" + goods_id,
      });
    },
    // 搜索历史 如果搜索的值在历史框没有就加在前面,有就删除之前的再加在前面
    saveHistory(keyword) {
      // findIndex返回的是索引值 如果item等于输入的值就返回它的索引赋值给index
      // const index = this.historyList.findIndex((item) => item === keyword);

      // 查找是否有存在的，如果有，则删掉
      // const index = this.historyList.findIndex((item) => item === keyword);
      // if (index > -1) {
      //   this.historyList.splice(index, 1);
      // }
      // 把关键字存储到historyList中
      this.historyList.unshift(keyword);

      uni.setStorageSync("history", this.historyList);
    },
    // 清空历史搜索
    clearHistory() {},
  },
};
</script>


<style lang="scss" scoped>
.search-container {
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
    ::v-deep .uni-searchbar {
      /* #ifndef APP-NVUE */
      display: flex;
      /* #endif */
      flex-direction: row;
      position: relative;
      padding: 16rpx;
      /* 将默认的 #FFFFFF 改为 #C00000 */
      background-color: #c00000;
    }

    .history-box {
      padding: 0 5px;

      .history-title {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 40px;
        font-size: 13px;
        border-bottom: 1px solid #efefef;
      }

      .history-list {
        display: flex;
        flex-wrap: wrap;

        ::v-deep .uni-tag {
          margin-top: 5px;
          margin-right: 5px;
        }
      }
    }

    .sugg-list {
      padding: 0 5px;

      .sugg-item {
        font-size: 12px;
        padding: 13px 0;
        border-bottom: 1px solid #efefef;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .goods-name {
          // 文字不允许换行（单行文本）
          white-space: nowrap;
          // 溢出部分隐藏
          overflow: hidden;
          // 文本溢出后，使用 ... 代替
          text-overflow: ellipsis;
          margin-right: 3px;
        }
      }
    }
  }

  .history-box {
    padding: 0 5px;

    .history-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 40px;
      font-size: 13px;
      border-bottom: 1px solid #efefef;
    }

    .history-list {
      display: flex;
      flex-wrap: wrap;

      ::v-deep .uni-tag {
        margin-top: 5px;
        margin-right: 5px;
      }
    }
  }
}
</style>
